package com.example.lp.lpuicore.anim;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.lp.lpuicore.R;

import java.util.ArrayList;
import java.util.List;

public class AnimActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_amin);
        initViews();
    }

    private void initViews() {
        List<BaseFragment> fragmentList = new ArrayList<>();
        TabLayout mTabLayout = findViewById(R.id.tab);
        ViewPager viewPager = findViewById(R.id.view_pager);
        fragmentList.add(instantiateFragment(viewPager, 0, new ViewAnimFragment()));
        fragmentList.add(instantiateFragment(viewPager, 1, new FrameFragment()));
        fragmentList.add(instantiateFragment(viewPager, 2, new PropertyAnimFragment()));
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragmentList));
        mTabLayout.setupWithViewPager(viewPager, false);

    }

    private BaseFragment instantiateFragment(ViewPager viewPager, int position, BaseFragment defaultResult) {
        String tag = "android:switcher:" + viewPager.getId() + ":" + position;
        BaseFragment fragment = (BaseFragment) getSupportFragmentManager().findFragmentByTag(tag);
        return fragment == null ? defaultResult : fragment;
    }
}
